/*
 * @Author: YiY
 * @Date: 2024-01-08 09:43:58
 * @LastEditTime: 2024-01-08 11:35:24
 */
// 非最简交换
function swap(list, a, b) {
  // 1.
  // console.log("a:", a, "b:", b);
  // let x = a - b
  // let y = b + a
  // b = (x + y) / 2
  // a = (y - x) / 2
  // console.log("a:", a, "b:", b)

  // 2.
  // let arr = [list[a], list[b]]
  // list[a] = arr[1]
  // list[b] = arr[0]
  // return list
}

let arr = [9, 0, 535, 43, 53, 53, 0, 43, 647, 54, 8, 7, 6, 5, 4, 3, 2, 1]

function Shell(arr) {
  let list = JSON.parse(JSON.stringify(arr))
  let len = list.length
  let gap = Math.floor(len / 2)

  while (gap > 0) {

    for (let i = gap; i < len; i++) {
      for (let j = i; list[j] < list[j - gap] && j > 0; j -= gap) {
        // 1. 是否小于前方值（初始必有值）//j值是gap是增量
        // 2. 向前递减
        // 3. 前方是否有值
        // j为0时,前方无值
        let temp = list[j]
        list[j] = list[j - gap]
        list[j - gap] = temp
        // swap(list, i, j - gap)
      }
    }

    gap = Math.floor(gap / 2)
  }

  console.log('list :>> ', list);
  return list
}

Shell(arr)